package com.crm.system.service.impl.orderManager;

import com.crm.common.model.DhPageInfo;
import com.crm.model.entity.contractManager.CustomerContract;
import com.crm.model.entity.orderManager.CustomerProductOrderDetail;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.orderManager.CustomerProductOrderVO;
import com.crm.model.vo.orderManager.OrderDetailDelVO;
import com.crm.model.vo.returnMoney.FinanceProductOderInfoVO;
import com.crm.service.contractManager.CustomerContractDbService;
import com.crm.service.messageprompt.MessagePromptDbService;
import com.crm.service.orderManager.CustomerProductOrderDbService;
import com.crm.service.orderManager.CustomerProductOrderDetailDbService;
import com.crm.system.service.orderManager.CustomerProductOrderDetailService;
import com.crm.system.service.orderManager.CustomerProductOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @ Author ：pym
 * @ Date   ：2021/3/17 14:38
 */
@Service
public class CustomerProductOrderDetailServiceImpl implements CustomerProductOrderDetailService {
    @Autowired
    CustomerProductOrderDetailDbService customerProductOrderDetailDbService;

    @Autowired
    private CustomerProductOrderDbService productOrderDbService;

    @Autowired
    private CustomerContractDbService contractDbService;

    @Autowired
    private CustomerProductOrderService customerProductOrderService;

    @Autowired
    private MessagePromptDbService messagePromptDbService;
    /**
     * 根据产品订单表主键和订单明细类型查询产品订单明细列表
     * @Author pym
     * @Date  2021/3/17 14:44
     * @param productOrderId -产品订单表主键
     * @param productOrderDetailType -订单明细类型--1:产品类型;2:活动类型;3:赠品活动类型
     * @return {@link {java.util.List<com.crm.model.entity.orderManager.CustomerProductOrderDetail>}}
     */
    @Override
    public List<CustomerProductOrderDetail> selectCustomerProductOrderDetailList(String productOrderId, String productOrderDetailType) {
        return customerProductOrderDetailDbService.selectCustomerProductOrderDetailList(productOrderId, productOrderDetailType);
    }

    /**
     * 删除订单产品
     * @param delVo
     */
    @Override
    public void delOrderDetail(OrderDetailDelVO delVo) {
        UserAccount user = (UserAccount) SecurityUtils.getSubject().getPrincipal();//获取登录人信息
        delVo.setUpdatePeopleId(user.getAccountId());
        delVo.setUpdateTm(new Date());
        customerProductOrderDetailDbService.delOrderDetail(delVo);
        //获取订单信息
        CustomerProductOrderVO order=productOrderDbService.getOrderInfoByNumber(delVo.getOrderNumber());
        if (Objects.nonNull(order)){
            FinanceProductOderInfoVO vo =customerProductOrderService.financeProductOderInfo(order.getProductOrderId(),1);
            order.setOrderActualTotalAmount(delVo.getResidueMoney());
            if (delVo.getResidueMoney().compareTo(BigDecimal.ZERO)==0 || vo.getProducts().size() < 1){
                order.setOrderStatus("3");
            }
            //更新订单表
            productOrderDbService.updateOrderComplete(order);
            //查合同
            CustomerContract contract=new CustomerContract();
            //查询回款次数
            contract.setContractId(order.getContractId());
            contract.setContractActualTotalAmount(delVo.getResidueMoney());
            if ("3".equals(order.getOrderStatus())){
                contract.setContractStatus("4");
            }
            //更新合同
            contractDbService.updateCustomerContract(contract);
            //退款完成插入一条消息
            messagePromptDbService.insertMsg(contract,2);
        }

    }

    /**
     * 根据订单标号查询退款记录
     * @return
     */
    @Override
    public DhPageInfo<OrderDetailDelVO> getDelOrderProductLogList(String orderId, Integer pageIndex, Integer pageSize) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<OrderDetailDelVO> list=customerProductOrderDetailDbService.getDelOrderProductLogList(orderId);
        PageInfo<OrderDetailDelVO> pageInfo=new PageInfo<>(list);
        return new DhPageInfo(pageInfo,list);
    }

    /**
     * 判断订单是否完成
     * @param orderNumber
     * @return
     */
    @Override
    public boolean ifOrderStatus(String orderNumber) {
        //获取订单信息
        CustomerProductOrderVO order=productOrderDbService.getOrderInfoByNumber(orderNumber);
        if(Objects.nonNull(order) && "2".equals(order.getOrderStatus())){
            return true;
        }
        return false;
    }
}
